import { createRouter, createWebHistory } from 'vue-router'
import IndexView from '@/views/admin/indexView.vue'
import LoginView from '@/views/loginView.vue'
import ElderlyView from '@/views/admin/ElderlyView.vue'
import MedicalStaffView from '@/views/admin/MedicalStaffView.vue'
import RoomView from '@/views/admin/RoomView.vue'
import AccountView from '@/views/admin/AccountView.vue'
import SalaryView from '@/views/admin/SalaryView.vue'
import HolidaysView from '@/views/admin/HolidaysView.vue'
import UserHomeView from '@/views/user/UserHomeView.vue'
import UserInfoView from '@/views/user/UserInfoView.vue'
import UserAccountView from '@/views/user/UserAccountView.vue'
import UserVisitorView from '@/views/user/UserVisitorView.vue'
import EmpHomeView from '@/views/emp/EmpHomeView.vue'
import EmpSalaryView from '@/views/emp/EmpSalaryView.vue'
import EmpHolidayView from '@/views/emp/EmpHolidayView.vue'
import EmpInfoView from '@/views/emp/EmpInfoView.vue'
import { useTokenStore } from '@/stores/token'
import UserDishesView from "@/views/user/UserDishesView.vue";
import UserRoomView from "@/views/user/UserRoomView.vue";
import UserRoomDetailView from "@/views/user/UserRoomDetailView.vue";
import UserServerView from "@/views/user/UserServerView.vue";
import HomeView from "@/views/admin/HomeView.vue";
import ServerView from "@/views/admin/ServerView.vue";
import CategoryView from "@/views/admin/CategoryView.vue";
import OrderView from "@/views/admin/OrderView.vue";
import GoodsView from "@/views/admin/GoodsView.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: 'index',
      children: [
        {
          path: '/eld',
          component: ElderlyView
        },
        {
          path: '/med',
          component: MedicalStaffView
        },
        {
          path: '/index',
          component: IndexView
        },
        {
          path: '/room',
          component: RoomView
        },
        {
          path: '/salary',
          component: SalaryView
        },
        {
          path: '/account',
          component: AccountView
        },
        {
          path: '/goods',
          component: GoodsView
        },
        {
          path: '/holidays',
          component: HolidaysView
        },
        {
          path: '/server',
          component: ServerView
        },
        {
          path: '/category',
          component: CategoryView
        },
        {
          path: '/order',
          component: OrderView
        }
      ]
    },
    {
      path: '/emp',
      name: 'emphome',
      component: EmpHomeView,
     
      children: [
        {
          path: '/empinfo',
          component:EmpInfoView
        },
        {
          path: '/empsalary',
          component: EmpSalaryView
        },
        {
          path: '/empholiday',
          component: EmpHolidayView
        }
      ]
    },
    {
      path: '/old',
      name: 'eldhome',
      component: UserHomeView,
      children: [
        {
          path: '/olddiskes',
          component: UserDishesView
        },
        {
          path: '/oldinfo',
          component: UserInfoView
        },
        {
          path: '/oldaccount',
          component: UserAccountView
        },
        {
          path: '/oldvisitor',
          component: UserVisitorView
        },
        {
          path: '/oldroom',
          component: UserRoomView
        },
        {
          path: '/roomdetail',
          component: UserRoomDetailView
        },
        {
          path: '/oldserver',
          component: UserServerView
        },
        {
          path: '/visitor',
          component: UserVisitorView
        },
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
  ]
});

//导航守卫
router.beforeEach((to, from) => {
  const tokenStore = useTokenStore();
  //to - 要访问的位置
  //from - 起始位置
  if (to.path == '/login') {
    return true;
  } else {
    //先判断是否有token
    if (!tokenStore.token) {
      return "/login";
    } else {
      return true;
    }
  }
});

export default router
